package com.yuandian.bpm.bi.modules.quartz.util;

import com.yuandian.bpm.bi.modules.quartz.service.impl.JobDeleteDataTaskTaskImpl;
import com.yuandian.bpm.bi.modules.quartz.service.impl.JobHeartbeatTaskImpl;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskInfo;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * <p> 项目启动时添加任务 </p>
 *
 * @Author: LinSheHi
 * @Date: 2022/9/27 17:42
 */
@Component
public class JobInitUtil implements ApplicationRunner {

	@Autowired
	private JobUtil jobUtils;

	private static JobUtil jobUtilsStatic;


	@PostConstruct
	public void init() {
		jobUtilsStatic = this.jobUtils;
	}

    @Override
    public void run(ApplicationArguments args) {

		// 初始化心跳任务
		String HEARTBEAT_taskName = "HEARTBEAT";
        ConfReportTaskInfo quartzBean = new ConfReportTaskInfo();
        quartzBean.setTaskCronExpression("*/30 * * * * ?");
        quartzBean.setJobName(HEARTBEAT_taskName);
        quartzBean.setJobGroup(HEARTBEAT_taskName);
        quartzBean.setTriggerName(HEARTBEAT_taskName);
        quartzBean.setTriggerGroup(HEARTBEAT_taskName);
		quartzBean.setTaskTotalExecuteCount(-1);
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("confReportTaskInfo", quartzBean);
		jobUtilsStatic.addJob(JobHeartbeatTaskImpl.class, jobDataMap, quartzBean);


		// 初始化数据删除任务
		String DELETE_DATE_taskName = "DELETE_DATE";
		ConfReportTaskInfo deleteQuartzBean = new ConfReportTaskInfo();
		deleteQuartzBean.setTaskCronExpression("0 0 0 * * ?");
		deleteQuartzBean.setJobName(DELETE_DATE_taskName);
		deleteQuartzBean.setJobGroup(DELETE_DATE_taskName);
		deleteQuartzBean.setTriggerName(DELETE_DATE_taskName);
		deleteQuartzBean.setTriggerGroup(DELETE_DATE_taskName);
		deleteQuartzBean.setTaskTotalExecuteCount(-1);
		JobDataMap jobDeleteDataMap = new JobDataMap();
		jobDeleteDataMap.put("confReportTaskInfo", deleteQuartzBean);
		jobUtilsStatic.addJob(JobDeleteDataTaskTaskImpl.class, jobDeleteDataMap, deleteQuartzBean);

    }
}

